<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    @close="closeDialog"
    @click.native="DialogClick"
  >
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <!-- value 要传输给后台的值  label 要显示的内容 -->
          <el-option
            v-for="item in obj.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          :value="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @click.native.stop="isShowTreeFn()"
        />
        <div v-if="isShowTree" class="tree-box">
          <el-tree
            v-loading="loading"
            :data="deptsList"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitUserItem()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetDepartments } from '@/api/departments'
import { recursionDepartsFn } from '@/utils/index'
import obj from '@/api/constant/employees' // 引入了一个常量文件
import { addEmployeeApi } from '@/api/employees'
import moment from 'moment'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      obj, // 聘用形式存储
      loading: false,
      isShowTree: false, // 控制树形部门显示和隐藏
      deptsList: [], // 部门数据
      // 表单数据
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 表单校验规则
      rules: {
        username: [
          {
            required: true,
            message: '用户姓名不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位',
            trigger: ['blur', 'change']
          }
        ],
        mobile: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: ['blur', 'change']
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: '聘用形式不能为空',
            trigger: ['blur', 'change']
          }
        ],
        workNumber: [
          {
            required: true,
            message: '工号不能为空',
            trigger: ['blur', 'change']
          }
        ],
        departmentName: [
          {
            required: true,
            message: '部门不能为空',
            trigger: ['blur', 'change']
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '请选择入职时间',
            trigger: ['blur', 'change']
          }
        ],
        correctionTime: [
          {
            required: true,
            message: '请选择转正时间',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    // this.getDepartments()
  },
  methods: {
    // 获取部门数据
    async getDepartments() {
      this.loading = true
      const {
        data: { depts }
      } = await reqGetDepartments()
      const deptsList = recursionDepartsFn(depts, '')
      this.deptsList = deptsList
      this.loading = false
      console.log(deptsList)
    },
    // 关闭弹层
    closeDialog() {
      this.$emit('update:showDialog', false)
      this.formData = {} // 清空表单数据
      this.$refs.addForm.resetFields() // 清空校验
    },
    // 点击显示属性部门数据
    isShowTreeFn() {
      this.isShowTree = !this.isShowTree
      if (!this.isShowTree) return
      console.log('点击了')
      this.getDepartments() // 请求数据
    },
    // 利用事件冒泡 关闭窗口
    // 整个dialog的数据 添加世界冒泡 但是阻止 阻止冒泡到儿子 不然会关闭弹窗打不开了
    DialogClick() {
      this.isShowTree = false
    },
    // el-tree 节点被点击时的回调

    nodeClick(data) {
      console.log(data)
      // 判断是不是叶子结点
      if (data.children && data.children.length > 0) {
        return
      }

      this.formData.departmentName = data.name
      this.isShowTree = false
    }, // 确定提交新增员工
    submitUserItem() {
      this.$refs.addForm.validate(async (flag) => {
        if (!flag) return
        // 处理时间格式
        this.formData.timeOfEntry = moment(this.formData.timeOfEntry).format(
          'YYYY-MM-DD'
        )
        this.formData.correctionTime = moment(
          this.formData.correctionTime
        ).format('YYYY-MM-DD')
        await addEmployeeApi(this.formData)
        this.$message.success('新增员工成功')
        this.$parent.getUserList() // 重新渲染表格数据 找到父组件调用父组件的方法
        this.closeDialog() // 新增完成之后 关闭添加弹窗
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
}
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
